package com.gjzt.business.media.controller;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.servlet.RequestAttribute;
import com.eastdigit.servlet.Response;
import com.gjzt.business.media.service.BusInfoSyncService;
import com.gjzt.business.media.service.BusPositionService;
import com.gjzt.business.media.service.MediaBusService;
import com.gjzt.business.media.service.MediaTypeService;

@Controller
@RequestMapping(value = "/media/bus")
public class MediaBusController {

    @Autowired
    private MediaBusService mediaBusService;

    @Autowired
    private BusInfoSyncService busInfoSyncService;

    @Autowired
    private MediaTypeService mediaTypeService;

    @Autowired
    private BusPositionService busPositionService;

    public static long lastRefreshBusTimes = 0;

    @RequestMapping(value = "/refresh")
    @ResponseBody
    public JSONObject refreshMediaBuses(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) throws Exception {
        int minite = 1;
        if (System.currentTimeMillis() - lastRefreshBusTimes >= minite * 60 * 1000) {
            lastRefreshBusTimes = System.currentTimeMillis();
            busInfoSyncService.loadBusInfo();
            return Response.responseSuccess();
        } else {
            long i = minite - (System.currentTimeMillis() - lastRefreshBusTimes) / 60 / 1000;
            return Response.responseError("请在" + i + "分钟后执行此操作");
        }
    }

    @RequestMapping(value = "/companys")
    @ResponseBody
    public JSONObject queryCompanys() {
        return Response.response(mediaBusService.queryCompanys());
    }

    /**
     * 查询车辆
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject queryMediaBus(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(mediaBusService.findMediaBus(reqBean));
    }

    @RequestMapping(value = "/{busId}/info")
    @ResponseBody
    public JSONObject getBusInfo(@PathVariable String busId) {
        return Response.response(mediaBusService.getMediaBusInfo(busId));
    }

    @RequestMapping(value = "/{busId}/infoWithRes")
    @ResponseBody
    public JSONObject getBusInfoWithRes(@PathVariable String busId) {
        return Response.response(mediaBusService.getBusInfoWithRes(busId));
    }

    /**
     * 查询车辆媒体类型
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/mediatypes")
    @ResponseBody
    public JSONObject queryBusMedias(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(mediaTypeService.queryBusMediaType());
    }

    /**
     * 保存车辆信息
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public JSONObject saveMediaBus(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        mediaBusService.saveMediaBus(reqBean);
        return Response.responseSuccess();
    }

    /**
     * 保存车辆信息
     * 
     * @param reqBean
     * @return
     */
    @RequestMapping(value = "/gps")
    @ResponseBody
    public JSONObject getBusGPS(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.responseSuccess(busPositionService.getLastBusPosition(reqBean.getInfo("vehicleNo"),
                reqBean.getInfo("lineNo")));// 自编号 线路号
    }

}